
package com.nl.controller.common.util;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public static String DEFAULT_FORMAT = "yyyy-MM-dd";

    /**
     * 获取当前年的第一天或最后一天
     *
     * @param type 0 第一天 其他最后一天
     * @return yyyy-MM-dd
     */
    public static String getCurrYearDay(int type) {
        Calendar currCal = Calendar.getInstance();
        int year = currCal.get(Calendar.YEAR);
        if (type == 0) {
            return year + "-01-01";
        } else {
            return year + "-12-31";
        }
    }

    /**
     * 获取当前月第一天
     *
     * @return yyyy-MM-dd
     */
    public static String getCurrMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return formatDate(calendar.getTime());
    }

    /**
     * 获取当月最后一天
     *
     * @return yyyy-MM-dd
     */
    public static String getCurrMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,
                calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return formatDate(calendar.getTime());
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String formatDate(Date date) {
        return new SimpleDateFormat(DEFAULT_FORMAT).format(date);
    }

    /**
     * 获取当前周的第一天或最后一天
     *
     * @param type 0 第一天 其他 最后一天
     * @return yyyy-MM-dd
     */
    public static String getCurrWeektDay(int type) {
        Calendar calendar = Calendar.getInstance();
        int day = 0;
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            day = -6;
        } else {
            day = 2 - calendar.get(Calendar.DAY_OF_WEEK);
        }
        calendar.add(Calendar.DAY_OF_WEEK, day);
        if (type == 0) {
            return formatDate(calendar.getTime());
        } else {
            calendar.add(Calendar.DAY_OF_WEEK, 6);
            return formatDate(calendar.getTime());
        }
    }

    /**
     * 获取当天日期
     *
     * @return yyyy-MM-dd
     */
    public static String getToday() {
        Calendar calendar = Calendar.getInstance();
        return formatDate(calendar.getTime());
    }

    public static String getLastDayOfMonth(String month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, Integer.parseInt(month.substring(0, 4)));
        //设置月份
        cal.set(Calendar.MONTH, Integer.parseInt(month.substring(4, 6)) - 1);

        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

    /**
     * 得到几天前的时间
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }

    /**
     * 得到几天后的时间
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        return now.getTime();
    }

    /**
     * 获取一个月的天数
     * @return
     */
    public static int getDayOfMonth(String yearMonth){
        int year=Integer.parseInt(yearMonth.substring(0,4));
        int month=Integer.parseInt(yearMonth.substring(5,6));

        Calendar time=Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR,year);
        //year年
        time.set(Calendar.MONTH,month-1);
        //Calendar对象默认一月为0,month月
        int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
        return day;
    }



}


